// 不要求两两模数互质，但不一定有解
// 思路：合并同余方程，复杂度nlog(v)
struct EXCRT {
  using i64 = long long;
  using i128 = __int128;

  i64 exgcd(i64 a, i64 b, i64 &x, i64 &y) {
    if (!b) {
      x = 1, y = 0;
      return a;
    }
    i64 d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
  }

  i64 work(vector<i64> a, vector<i64> p) {
    assert(a.size() == p.size() && (int)a.size() > 0);
    i128 M = p[0], ret = a[0];
    i64 x, y;

    for (int i = 1; i < a.size(); i++) {
      i128 res = ((a[i] - ret) % p[i] + p[i]) % p[i];
      i64 g = exgcd(M, p[i], x, y);
      if (res % g) {
        return -1;
      }
      x = (i64)((i128)x * (res / g) % p[i]);
      ret += x * M;
      M = (i128)p[i] / g * M;
      ret = (ret % M + M) % M;
    }
    return (i64)(ret);
  }
};
void solve()
{
  int n;
  cin >> n;
  vector<long long> a(n), p(n);

  for (int i = 0; i < n; i++) {
    cin >> p[i] >> a[i];
  }
  EXCRT crt;
  cout << crt.work(a, p);
} 